# 使用官方 code-server 镜像作为基础
FROM docker.1panel.live/codercom/code-server:latest

# 设置默认安装插件
ENV EXTENSIONS="ms-python.python,ms-python.debugpy"

# 切换到 root 用户安装编译和调试环境
USER root

# 创建默认工作区目录
RUN mkdir -p /config/workspace/.vscode

# 生成 launch.json 文件
RUN cat > /config/workspace/.vscode/launch.json <<'EOF'
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "g++ build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}
EOF

# 生成 tasks.json 文件
RUN cat > /config/workspace/.vscode/tasks.json <<'EOF'
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ]
}
EOF

# 配置 code-server 的主题和字体
RUN cat > /config/workspace/.vscode/settings.json <<'EOF'
{
    "editor.fontSize": 17,
    "workbench.colorTheme": "Default Dark+",
    "editor.fontFamily": "Fira Code, Consolas, 'Courier New', monospace",
    "editor.fontLigatures": true
}
EOF

# 生成 test.cpp 示例文件
RUN cat > /config/workspace/test.cpp <<'EOF'
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    ll n; cin >> n;
    cout << "Hello World\n";
    int a = a >= 1;
    return 0;
}
EOF

# 生成 test.py 示例文件
RUN cat > /config/workspace/test.py <<'EOF'
i = input()
print("hello world")
EOF

# 复制默认配置到 /defaults 目录
RUN rm -rf /defaults && \
    mkdir -p /defaults && \
    cp -r /config/workspace /defaults/workspace


    # 创建启动脚本 start.sh
RUN cat > /usr/local/bin/start.sh <<'EOF'
#!/bin/bash
set -e

# 配置APT镜像源为清华大学镜像站
touch /etc/apt/sources.list
cat <<'EOL' > /etc/apt/sources.list
# 默认注释了源码镜像以提高 apt update 速度，如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware

deb https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
# deb-src https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
EOL
echo "配置APT清华大学镜像站成功"

# 更新APT缓存并安装依赖
echo "正在更新APT缓存并安装依赖"
apt-get update && \
apt-get install -y build-essential gdb python3 python3-pip unzip fontconfig git && \
rm -rf /var/lib/apt/lists/*
echo "依赖安装成功"

# 下载并安装Fira Code字体
echo "正在下载并安装Fira Code字体..."
mkdir -p /config/workspace && \
wget https://github.com/tonsky/FiraCode/releases/download/6.2/Fira_Code_v6.2.zip && \
unzip Fira_Code_v6.2.zip -d FiraCode && \
mkdir -p ~/.local/share/fonts/FiraCode && \
cp FiraCode/ttf/*.ttf ~/.local/share/fonts/FiraCode/ && \
fc-cache -fv && \
rm -rf Fira_Code_v6.2.zip FiraCode
echo "安装Fira Code字体成功"

# 下载并安装VS Code的C++扩展
echo "正在下载并安装C++扩展..."
wget -O /config/workspace/cpptools-linux-x64.vsix https://github.com/microsoft/vscode-cpptools/releases/latest/download/cpptools-linux-x64.vsix && \
code-server --install-extension /config/workspace/cpptools-linux-x64.vsix && \
rm -rf /config/workspace/cpptools-linux-x64.vsix
echo "安装C++扩展成功"

# 安装扩展
echo "正在下载并安装其余扩展..."
if [ -n "$EXTENSIONS" ]; then
IFS=',' read -ra exts <<< "$EXTENSIONS"
for ext in "${exts[@]}"; do
    echo "正在下载: $ext"
    code-server --install-extension "$ext"
done
echo "安装其余扩展成功，code-server即将启动..."
fi

# 检查 /config/workspace 是否存在内容，如果没有则复制默认工作区
if [ ! -d "/config/workspace" ] || [ -z "$(find /config/workspace -mindepth 1 -maxdepth 1 2>/dev/null)" ]; then
    echo "用户无本地配置，复制默认配置至默认工作区"
    mkdir -p /config
    cp -r /defaults/workspace /config/
fi

# 根据是否设置 PASSWORD 决定认证模式
if [ -n "$PASSWORD" ]; then
    AUTH_MODE="password"
else
    AUTH_MODE="none"
fi

exec code-server --bind-addr 0.0.0.0:8080 --auth $AUTH_MODE /config/workspace
EOF
    

# 赋予启动脚本执行权限
RUN chmod +x /usr/local/bin/start.sh

# 暴露 code-server 默认端口
EXPOSE 8080

# 设置启动脚本为入口点
ENTRYPOINT ["/usr/local/bin/start.sh"]